package edu.wy.smartcaneadmin;

import com.alibaba.fastjson2.JSON;
import edu.wy.smartcaneadmin.pojo.dto.resp.ClientRespData;
import edu.wy.smartcaneadmin.pojo.dto.resp.IotResponse;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Client {
    public static void main(String[] args) throws Exception {
        // 设备信息
        String productId = "97100ZyNhy";
        String deviceName = "test1";
        String apikey = "version=2022-05-01&res=userid%2F424311&et=1743417422&method=sha1&sign=KGCCwYD%2B%2BvDDhlyivcR5lg5Mkmc%3D";
        // 构造请求URL
        String apiUrl = "https://iot-api.heclouds.com/device/detail";
        String url = apiUrl + "?product_id=" + productId + "&device_name=" + deviceName;
        // 创建URL对象
        URL obj = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) obj.openConnection();
        // 设置请求方法为GET
        connection.setRequestMethod("GET");
        // 设置请求头
        connection.setRequestProperty("Authorization", apikey);
        // 获取响应
        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        // 打印响应结果
        System.out.println("Response: " + response.toString());

        // 使用 Fastjson2 解析响应
        IotResponse iotResponse = JSON.parseObject(response.toString(), IotResponse.class);
        ClientRespData data = iotResponse.getData();

        // 打印 data 部分的数据
        System.out.println("Device ID: " + data.getDid());
        System.out.println("Product ID: " + data.getPid());
        // 可以继续添加其他属性的打印
    }
}    